/
TimePeriods.php
170 lines (142 loc) 路 4.37 KB
/
TimePeriods.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
namespace Aeon\Calendar\Gregorian;
/**
* @implements \IteratorAggregate<TimePeriod>
*/
final class TimePeriods implements \Countable, \IteratorAggregate
{
/**
* @var \Iterator<TimePeriod>
*/
private \Iterator $periods;
/**
* @param \Iterator<TimePeriod> $periods
*/
private function __construct(\Iterator $periods)
{
$this->periods = $periods;
}
public static function fromArray(TimePeriod ...$periods) : self
{
return new self(new \ArrayIterator($periods));
}
public static function fromIterator(TimePeriodsIterator $timePeriodsIterator) : self
{
return new self($timePeriodsIterator);
}
/**
* @return array<TimePeriod>
*/
public function all() : array
{
return \iterator_to_array($this->periods);
}
/**
* @psalm-param pure-callable(TimePeriod $timePeriod) : void $iterator
*
* @param callable(TimePeriod $timePeriod) : void $iterator
*/
public function each(callable $iterator) : void
{
foreach ($this->periods as $period) {
$iterator($period);
}
}
/**
* @psalm-param pure-callable(TimePeriod $timePeriod) : mixed $iterator
*
* @param callable(TimePeriod $timePeriod) : mixed $iterator
*
* @return array<mixed>
*/
public function map(callable $iterator) : array
{
return \array_map($iterator, $this->all());
}
/**
* @psalm-param pure-callable(TimePeriod $timePeriod) : bool $iterator
*
* @param callable(TimePeriod $timePeriod) : bool $iterator
*
* @return self
*/
public function filter(callable $iterator) : self
{
return new self(new \CallbackFilterIterator($this->periods, $iterator));
}
public function count() : int
{
return \iterator_count($this->periods);
}
public function getIterator() : \Traversable
{
return $this->periods;
}
/**
* Find all gaps between time periods.
*/
public function gaps() : self
{
$periods = \array_map(
function (TimePeriod $timePeriod) : TimePeriod {
return $timePeriod->isBackward() ? $timePeriod->revert() : $timePeriod;
},
$this->sort()->all()
);
$gaps = [];
$totalPeriod = \current($periods);
while ($period = \next($periods)) {
if ($totalPeriod->overlaps($period) || $totalPeriod->abuts($period)) {
$totalPeriod = $totalPeriod->merge($period);
} else {
$gaps[] = new TimePeriod($totalPeriod->end(), $period->start());
$totalPeriod = $period;
}
}
return self::fromArray(...$gaps);
}
public function sort() : self
{
return $this->sortBy(TimePeriodsSort::asc());
}
public function sortBy(TimePeriodsSort $sort) : self
{
$periods = $this->all();
\uasort(
$periods,
function (TimePeriod $timePeriodA, TimePeriod $timePeriodB) use ($sort) : int {
if ($sort->byStartDate()) {
return $sort->isAscending()
? $timePeriodA->start()->toDateTimeImmutable() <=> $timePeriodB->start()->toDateTimeImmutable()
: $timePeriodB->start()->toDateTimeImmutable() <=> $timePeriodA->start()->toDateTimeImmutable();
}
return $sort->isAscending()
? $timePeriodA->end()->toDateTimeImmutable() <=> $timePeriodB->end()->toDateTimeImmutable()
: $timePeriodB->end()->toDateTimeImmutable() <=> $timePeriodA->end()->toDateTimeImmutable();
}
);
return self::fromArray(...$periods);
}
public function first() : ?TimePeriod
{
$this->periods->rewind();
return $this->periods->current();
}
public function last() : ?TimePeriod
{
$periods = $this->all();
if (!\count($periods)) {
return null;
}
return \end($periods);
}
public function add(TimePeriod ...$timePeriods) : self
{
return self::fromArray(...\array_merge($this->all(), $timePeriods));
}
public function merge(self $timePeriods) : self
{
return self::fromArray(...\array_merge($this->all(), $timePeriods->all()));
}
}